import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Main",
    component: () => import("@c/layout/Main"),
    meta: {
      title: "首页"
    },
    children: [
      {
        path: "/message",
        name: "message",
        component: () => import("@v/message/List.vue"),
        meta: {
          title: "Message List",
          keepAlive: true
        }
      },
      {
        path: "/permissions",
        name: "permissions",
        component: () => import("@v/permissions/List.vue"),
        meta: {
          title: "Permissions List",
          keepAlive: true
        }
      },
      {
        path: "/classified",
        name: "classified",
        component: () => import("@v/classified/List.vue"),
        meta: {
          title: "Classified List",
          keepAlive: true
        }
      },
      {
        path: "/classified/edit",
        name: "classified-edit",
        component: () => import("@v/classified/Edit.vue"),
        meta: {
          title: "Classified Edit",
          keepAlive: false
        }
      },
      {
        path: "/role",
        name: "role",
        component: () => import("@v/role/List.vue"),
        meta: {
          title: "Role List",
          keepAlive: true
        }
      },
      {
        path: "/role/add",
        name: "role-add",
        component: () => import("@v/role/Add.vue"),
        meta: {
          title: "Role Add",
          keepAlive: false
        }
      },
      {
        path: "/role/edit",
        name: "role-edit",
        component: () => import("@v/role/Edit.vue"),
        meta: {
          title: "Role Edit",
          keepAlive: false
        }
      },
      {
        path: "/role/author",
        name: "role-author",
        component: () => import("@v/role/Author.vue"),
        meta: {
          title: "Role Authorization",
          keepAlive: false
        }
      }
    ]
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@v/Login.vue")
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

router.beforeEach((to, from, next) => {
  store.dispatch("tagsView/setActiveMenuName", to.path);
  if (store.state.user.userInfo === null && to.path !== "/login") {
    next("/login");
  } else {
    next();
  }
});

export default router;
